<?php
namespace app\wechat\controller;
use Des3\DES3;
use think\Session;

class Set extends Common
{
    public function index()
    {
        return view();
    }

    public function account()
    {
        $param = [
            'member_id' => session('member_id')
        ];
        $data = call_interface('get_set_account_info', $param);
        $this->assign('data', $data['data']);
        return view();
    }

    public function about()
    {
        $this->assign('type', input('param.type'));
        return view();
    }

    public function verify() {
        $id = session('member_id');
        $action = input('param.action');
        $member = db('member')->field('phone')->where('id', $id)->find();

        $this->assign('phone', $member['phone']);
        $this->assign('action', $action);
        $this->assign('type', input('param.type'));
        return view();
    }

    // 修改手机号码
    public function update_phone() {
        $check_code = input('param.check_code');
        if (request()->isAjax()) {
            if (cookie($check_code) != 1) {
                return json(['code' => 0, 'msg' => '系统繁忙！！']);
            }
            $code = input('param.code');
            $des = new DES3();
            $param = [
                'member_id' => session('member_id'),
                'code' => $code,
                'phone' => $des->encrypt(input('param.phone'))
            ];
            $data = call_interface('update_phone', $param);
            if ($data['code'] == 200) {
                session('phone', input('param.phone'));
            }
            return json($data);
        } else {
            if (cache('check_code_status') != 1) {
                return view('public/error');
            }
            cache('check_code_status', 0);
            $this->assign('type', input('param.type'));
            return view();
        }
    }

    // 设置安全密码
    public function save_safe_password() {
        if (request()->isAjax()) {
            $code = input('param.code');
            if (cookie($code) != 1) {
                return json(['code' => 0, 'msg' => '身份验证失败！！']);
            }
            $des = new DES3();
            $param = [
                'member_id' => session('member_id'),
                'safe_password' => $des->encrypt(input('param.safe_password'))
            ];
            $data = call_interface('save_safe_password', $param);
            if ($data['code'] == 200) {
                cookie($code, null);
            }
            return json($data);
        } else {
//            if (cookie($code) != 1) {
//                return view('public/error');
//            }
//            $this->assign('code', $code);
            //cookie($code, null);
            $this->assign('type', input('param.type'));
            $this->assign('phone', session('phone'));
            return view();
        }
    }

    // 修改登录密码
    public function update_password() {
        if (request()->isAjax()) {
            $des = new DES3();
            $param = [
                'member_id' => session('member_id'),
                'password' => $des->encrypt(input('param.password')),
                'new_password' => $des->encrypt(input('param.new_password')),
                're_new_password' => $des->encrypt(input('param.re_new_password'))
            ];
            $data = call_interface('update_password', $param);
            return json($data);
        } else {
            $this->assign('type', input('param.type'));
            //$this->assign('phone', session('phone'));
            return view();
        }
    }

    // 退出登录
    public function logout() {
        $member_id = session('member_id');

        try {
            // 解除帐号微信绑定
            db('member')->where('id', $member_id)->update(['openid' => '', 'band_status' => 2]);

            // 清空session
            Session::clear();

            return json(['code' => 200, 'msg' => 'SUCCESS']);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
}
